package org.tgcloud.group.web.controller.manager.project;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.tgcloud.group.core.APPResultMessage;
import org.tgcloud.group.core.custom.Permissions;
import org.tgcloud.group.core.session.Session;
import org.tgcloud.group.service.manager.project.FileManageService;
import org.tgcloud.group.web.controller.dto.project.file.ProjectFileDTO;
import org.tgcloud.group.web.controller.manager.ManageAPPBaseController;
import org.tgcloud.group.web.controller.req.project.file.FileAddReq;
import org.tgcloud.group.web.controller.req.project.file.FileIdReq;
import org.tgcloud.group.web.controller.req.project.file.FileRejectReq;
import org.tgcloud.group.web.controller.req.project.file.FileSearchReq;

/**
 * @Author: zhy
 * @Created: 2025-09-05-上午11:00
 * @Description:
 */
@Api(tags = "项目文件管理相关接口")
@RestController
@RequestMapping("/manager/pc/user")
@EnableAutoConfiguration
public class ProjectFileManageController extends ManageAPPBaseController {

    @Autowired
    private FileManageService fileManageService;


    @Permissions("projectFile:view")
    @ApiOperation(value = "项目文件列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string"),
    })
    @ResponseBody
    @RequestMapping(value = "/getProjectFileList", method = RequestMethod.POST)
    public APPResultMessage<Object, ProjectFileDTO> getProjectFileList(@ModelAttribute("session") Session session,
                                                                       @RequestBody FileSearchReq req) {
        return fileManageService.getProjectFileList(req.getPageNum(),req.getPageSize(), req.getProjectId(), req.getKeyword(), req.getFileType(), req.getRejectStatus(),
                req.getCreateStartTime(), req.getCreateEndTime(), req.getRejectStartTime(), req.getRejectEndTime(),req.getStageId());
    }


    @ApiOperation(value = "上传项目文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string"),
    })
    @ResponseBody
    @RequestMapping(value = "/doAddProjectFile", method = RequestMethod.POST)
    public APPResultMessage doAddProjectFile(@ModelAttribute("session") Session session,
                                             @RequestPart("file") MultipartFile file,
                                             @RequestParam Integer fileType,
                                             @RequestParam Integer projectId,
                                             @RequestParam(required = false) String remark) throws Exception{
        return fileManageService.doAddProjectFile(file, projectId, fileType, remark, session.getUserId(), session.getUserType());
    }


    @Permissions("projectFile:reject")
    @ApiOperation(value = "文件驳回")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string"),
    })
    @ResponseBody
    @RequestMapping(value = "/rejectFile", method = RequestMethod.POST)
    public APPResultMessage doRejectFile(@ModelAttribute("session") Session session,
                                         @RequestBody FileRejectReq req) {
        return fileManageService.doRejectFile(req.getId(), req.getRejectReason(), session.getUserId(), session.getUserType());
    }

    @Permissions("project:rejectPackage")
    @ApiOperation(value = "重新配包")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string"),
    })
    @ResponseBody
    @RequestMapping(value = "/rePackage", method = RequestMethod.POST)
    public APPResultMessage rePackage(@ModelAttribute("session") Session session,
                                         @RequestBody FileRejectReq req) {
        return fileManageService.rePackage(req.getProjectId(), req.getRejectReason(), session.getUserId(), session.getUserType());
    }






    @ApiOperation(value = "文件下载")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string"),
    })
    @ResponseBody
    @RequestMapping(value = "/downloadProjectFile", method = RequestMethod.POST)
    public ResponseEntity<byte[]> downloadFile(@RequestParam String resourceUrl) {
        return fileManageService.downloadFile(resourceUrl);
    }


    @Permissions("projectFile:sureFinal,project:surePackage")
    @ApiOperation(value = "确认终版文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string"),
    })
    @ResponseBody
    @RequestMapping(value = "/sureFinalFile", method = RequestMethod.POST)
    public APPResultMessage doSureFinalFile(@ModelAttribute("session") Session session,
                                            @RequestBody FileIdReq req) {
        return fileManageService.doSureFinalFile(req.getId(), session.getUserId(), session.getUserType());
    }


    @Permissions("projectFile:channelFinal")
    @ApiOperation(value = "取消终版文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string"),
    })
    @ResponseBody
    @RequestMapping(value = "/channelFinalFile", method = RequestMethod.POST)
    public APPResultMessage doChannelFinalFile(@ModelAttribute("session") Session session,
                                               @RequestBody FileIdReq req) {
        return fileManageService.doChannelFinalFile(req.getId(), session.getUserId(), session.getUserType());
    }

//    @Permissions("projectFile:channelFinal")
    @ApiOperation(value = "获取终版文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string"),
    })
    @ResponseBody
    @RequestMapping(value = "/getFinalFile", method = RequestMethod.POST)
    public APPResultMessage getFinalFile(@ModelAttribute("session") Session session,
                                               @RequestBody FileIdReq req) {
        return fileManageService.getFinalFile(req, session.getUserId(), session.getUserType());
    }

}
